package com.aiks.dsyncpluginspringbootautoconfigure.plugin;


import com.aiks.dsyncpluginspringbootautoconfigure.ClientDeptService;
import com.aiks.dsyncpluginspringbootautoconfigure.SpringContext;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.ModuleConfigEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgPlugParamsEntity;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * AbstractFilterDeptConditionPlugin.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @date 2022-07-06 16:46
 * @since 1.0.0
 **/
public abstract class AbstractFilterDeptConditionPlugin<T> extends AbstractConditionPlugin<T> {
    /**
     * doFilter.
     *
     * @param moduleConfig          moduleConfig
     * @param t                     t
     * @param orgPlugParamsEntities orgPlugParamsEntities
     * @return Boolean
     */
    @Override
    protected Boolean doFilter(final ModuleConfigEntity moduleConfig, final T t,
                               final List<OrgPlugParamsEntity> orgPlugParamsEntities) {
        ClientDeptService clientDeptService = SpringContext.getAppContext().getBean(ClientDeptService.class);
        for (OrgPlugParamsEntity orgPlugParamsEntity : orgPlugParamsEntities) {
            Object fieldValue = ReflectUtils.INSTANCE.getFieldValue(t, orgPlugParamsEntity.getSaasFieldName());
            List<String> handleValues = Lists.newArrayList(orgPlugParamsEntity.getHandleValues().trim().split(","));
            List<String> stringList = clientDeptService.recursiveDownContainYourself(moduleConfig.getOrgId(), handleValues);
            if (stringList.contains(String.valueOf(fieldValue))) {
                return judgeResult();
            }
        }
        return !judgeResult();
    }
    
    /**
     * judgeResult.
     *
     * @return boolean
     */
    protected abstract boolean judgeResult();
}
